<?php

namespace bc\model;

use bc\ar\MActiveRecord;

class ItemModel extends \bc\ar\MActiveRecord{
    
/**
     * bc_item表
     */
    public static function tableName()
   {
		return '{{%item}}'; 
   }
   
   /*
    * 通过商品id获取项目信息
    */
    public function getItemByProductId($id)
    {
        $this->columns = "bc_item.*";
        $ar = $this->find()->select($this->columns)->leftJoin('bc_item_product','bc_item_product.item_id=bc_item.id')->where(['bc_item_product.product_id'=> $id]);
        $list =  $ar->asArray()->all ();
        
        $this->columns = "bc_subitem.*,bc_item.item_name";
        $ar = $this->find()->select($this->columns)->rightJoin('bc_subitem','bc_subitem.item_id=bc_item.id')->leftJoin('bc_item_product','bc_item_product.item_id=bc_item.id')->where(['bc_item_product.product_id'=> $id]);
        // echo $ar->createCommand()->getRawSql();
        //SELECT `bc_subitem`.* FROM `bc_item` RIGHT JOIN `bc_subitem` ON bc_subitem.item_id=bc_item.id LEFT JOIN `bc_item_product` ON bc_item_product.item_id=bc_item.id WHERE `bc_item_product`.`product_id`=2
        $Childlist =  $ar->asArray()->all ();
              
        return array($list,$Childlist);

       
    }   
        public function getItemTypeByProductId($id)
   {    
        $this -> columns = "item_type";
        $ar = $this->find()->select($this->columns)
                ->where(['bc_item.id' => $id]);
        //echo $ar->createCommand()->getRawSql();
        
        $item_type = $ar->asArray()->one();
        return $item_type['item_type'];
       
   }

    /*
     * 获取用户基本信息
     * userid:用户id
     * productid:产品id
     * */
    public function getUserInfoByUseridAndProductId($userid,$productid,$bind_no)
    {
        if(empty($userid)&&empty($productid))
            return array();

        $whereArr=array();
        $whereArr['bc_item_data.user_id']=$userid;
        $whereArr['bc_item_product.product_id']=$productid;

        //通过bind_no获取绑定bind_id
        $bindM = new BindModel();
        $bindData = $bindM ->getRow(['bind_no'=>$bind_no]);
        $whereArr['bc_item_data.bind_id']=$bindData['bind_id'];
        $this -> columns = "bc_item.id as biid,bc_item.item_type as biitem_type,bc_item_product.id as bipid,bc_item_data.id as bidid,bc_item_detail.id as bideid,bc_subitem.id as bsid,bc_subitem.item_type as bsitem_type,bc_item.*,bc_item_product.*,bc_item_data.*,bc_item_detail.*,bc_subitem.*";
        $ar = $this->find()->select($this->columns)->from('bc_item_product')
            ->leftJoin('bc_item','bc_item.id = bc_item_product.item_id')
            ->leftJoin('bc_item_data','bc_item_data.item_product_id = bc_item_product.item_id')
            ->leftJoin('bc_item_detail','bc_item_detail.item_data_id = bc_item_data.id')
            ->leftJoin('bc_subitem','bc_subitem.id = bc_item_detail.subitem_id')
            ->where($whereArr);
//        echo $ar->createCommand()->getRawSql();
//        die();
        $UserInfoArr = $ar->asArray()->all();
        return $UserInfoArr;
    }
}